package zua.program.androidstudioprogram01;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class QuestionActivity extends AppCompatActivity {
    Button btn_A,btn_B,btn_next,btn_look_answer,btn_prev;//生成一个可以控制xml界面控件的对象
    TextView tv_problem,petName_name;  //
    Question questions[]; //题目数组
    int count = 0;  //记录题号
    int grade = 0;  //记录成绩
    String str = null;
    private final int MainToCheck = 520;
    private final int CheckToMain = 1314;
    private final int AlreadyLookAnswer = 1010;

    //保存函数
    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("count",count);
    }

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question);
        loadQuestion();

        //将定义的值与其在xml中对应的id绑定
        petName_name = findViewById(R.id.petName_name);
        btn_A = findViewById(R.id.button_A);
        btn_B = findViewById(R.id.button_B);
        btn_next = findViewById(R.id.button_next);
        btn_prev = findViewById(R.id.button_prev);
        btn_look_answer = findViewById(R.id.button_look_answer);
        tv_problem = findViewById(R.id.textView_problem);

        Intent intent = getIntent();
        str = intent.getStringExtra("username");
        //count = Integer.parseInt(intent.getStringExtra("count"));

        //判断目前保存状态
        if (savedInstanceState != null) {
            count = savedInstanceState.getInt("count");
            tv_problem.setText(questions[count].getQuestion());
        }

        //设置各个按钮的监听器
        btn_A.setOnClickListener(new View.OnClickListener() {   //按钮A的监听器
            @Override
            public void onClick(View view) {
                if (questions[count%questions.length].isCheck == true) {     //如果用户看过答案，就让按钮停止工作
                    btn_A.setEnabled(false); //不能工作
                } else { //检查答案对错
                    String str1 = btn_A.getText().toString();
                    String str2 = questions[count%questions.length].getAnswer();
                    CheckAnswer(str,str1,str2);
                    btn_A.setEnabled(false);
                    btn_B.setEnabled(false);
                }
            }
        });
        btn_B.setOnClickListener(new View.OnClickListener() {   //按钮B的监听器
            @Override
            public void onClick(View view) {
                if (questions[count%questions.length].isCheck == true) {      //如果用户看过答案，就让按钮停止工作
                    btn_B.setEnabled(false);  //不能工作
                } else { //检查答案对错
                    String str1 = btn_B.getText().toString();
                    String str2 = questions[count%questions.length].getAnswer();
                    CheckAnswer(str,str1,str2);
                    btn_A.setEnabled(false);
                    btn_B.setEnabled(false);
                }
            }
        });
        btn_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //具体实现
                count++;
                tv_problem.setText(questions[count%questions.length].getQuestion());
                questions[count%questions.length].isCheck = false; //没看过答案，就让按钮正常工作
                btn_A.setEnabled(true);
                btn_B.setEnabled(true);
                if (count == questions.length-2 || count == questions.length-1) {
                    btn_B.setEnabled(false);
                }
            }
        });
        btn_prev.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //具体实现
                if (count == 0) {
                    count = questions.length-1;
                } else {
                    count--;
                }
                tv_problem.setText(questions[count%questions.length].getQuestion());
                questions[count%questions.length].isCheck = false; //没看过答案，就让按钮正常工作
                btn_A.setEnabled(true);
                btn_B.setEnabled(true);
            }
        });
        btn_look_answer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //显示Intent,实现界面之间的跳转，不传递数据  从哪一个页面跳转到哪一个页面
//                Intent intent1 = new Intent(MainActivity.this,CheckActivity.class);
//                intent1.putExtra("answer",questions[count%questions.length].getAnswer());//把答案传过去
//                startActivity(intent1);   //启动
                //显示Intent,实现界面之间的跳转，传递数据
                Intent intent1 = new Intent(QuestionActivity.this,CheckActivity.class);
                intent1.putExtra("answer",questions[count%questions.length].getAnswer());//把答案传过去
                intent1.putExtra("username",str);
                intent1.putExtra("count",count);
                startActivityForResult(intent1,520);
            }
        });
    }

    //检查用户是否已经查看答案
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Intent intent2 = getIntent();
        int a = Integer.parseInt(intent2.getStringExtra("alreadyLookAnswer"));
        //requestCode 请求码    resultCode 结果返回码
        if ((requestCode == MainToCheck && resultCode == CheckToMain) || a == AlreadyLookAnswer) {
            questions[count%questions.length].isCheck = true;//如果请求码和结果码都和设置的相同，就说明用户已经看过答案啦~~~
            Toast.makeText(QuestionActivity.this,"不要贪心，你已经看过答案啦",Toast.LENGTH_LONG).show();
        } else {//相反说明没看过
            Toast.makeText(QuestionActivity.this,"你还没有看过答案，加油自己做哦",Toast.LENGTH_LONG).show();
        }
        //else if (intent1.getStringExtra("alreadyLookAnswer") == AlreadyLookAnswer)
    }

    //检查答案是否正确并弹出弹框
    private <string> void CheckAnswer(String str,string str1,string str2) {
        //弹窗(当前的弹框要在哪个activity,弹框的文本,弹框的时间有多长)  .show()用来显示
        //Toast.maoast.LENGTH_LONG).show();
        if (str1.equals(str2)) {
            grade = grade + questions[count].getScore();
            questions[count].setScore(0);//加过的分在第二次答题时不加分
            Toast.makeText(QuestionActivity.this,"恭喜你答对啦",Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(QuestionActivity.this,"请点击旁边的正确答案",Toast.LENGTH_LONG).show();
        }
        if (count == questions.length-1) {
            btn_A.setEnabled(false);
            btn_B.setEnabled(false);
            btn_look_answer.setEnabled(false);
            btn_next.setEnabled(false);
            btn_prev.setEnabled(false);
            Toast.makeText(QuestionActivity.this,"恭喜你的得分是"+grade,Toast.LENGTH_LONG).show();
            Intent intent = new Intent(QuestionActivity.this,PersonaInformationActivity.class);
            intent.putExtra("username", str);
            startActivity(intent);
        }
    }


    public void loadQuestion() {
        questions = new Question[] {
                new Question(getResources().getString(R.string.problem1),getResources().getString(R.string.answer1)),
                new Question(getResources().getString(R.string.problem2),getResources().getString(R.string.answer2)),
                new Question(getResources().getString(R.string.problem3),getResources().getString(R.string.answer3)),
                new Question(getResources().getString(R.string.problem4),getResources().getString(R.string.answer4)),
                new Question(getResources().getString(R.string.problem5),getResources().getString(R.string.answer5)),
                new Question(getResources().getString(R.string.problem6),getResources().getString(R.string.answer6)),
                new Question(getResources().getString(R.string.problem7),getResources().getString(R.string.answer7)),
                new Question(getResources().getString(R.string.problem8),getResources().getString(R.string.answer8)),
                new Question(getResources().getString(R.string.problem9),getResources().getString(R.string.answer9)),
                new Question(getResources().getString(R.string.problem10),getResources().getString(R.string.answer10)),
                new Question(getResources().getString(R.string.problem11),getResources().getString(R.string.answer12)),
                new Question(getResources().getString(R.string.problem12),getResources().getString(R.string.answer12)),
        };
    }
}
